/*
 *代码由框架生成,此处任何更改都可能导致被代码生成器覆盖
 *所有业务编写全部应在Partial文件夹下IngredientsAreaService与IIngredientsAreaService中编写
 */
using Mercury.Business.IRepositories;
using Mercury.Business.IServices;
using Mercury.Core.BaseProvider;
using Mercury.Core.Extensions.AutofacManager;
using Mercury.Entity.DomainModels;

namespace Mercury.Business.Services
{
    public partial class IngredientsAreaService : ServiceBase<IngredientsArea, IIngredientsAreaRepository>
    , IIngredientsAreaService, IDependency
    {
        public IngredientsAreaService(IIngredientsAreaRepository repository)
        : base(repository)
        {
            Init(repository);
        }
        public static IIngredientsAreaService Instance
        {
            get { return AutofacContainerModule.GetService<IIngredientsAreaService>(); }
        }

        public override PageGridData<IngredientsArea> GetPageData(PageDataOptions options)
        {
            var result = base.GetPageData(options);
            if (result.rows.Any())
            {
                var list = new List<string>();
                list.AddRange(result.rows.Select(c => c.MaterialPreparationMaterials));
                list.AddRange(result.rows.Select(c => c.BatchingMaterials));
                var materials = repository.Find<MaterialMaster>(c => list.Contains(c.Id));
                result.rows.ForEach(c =>
                {
                    var material = materials.FirstOrDefault(x => x.Id == c.MaterialPreparationMaterials);
                    if (material != null)
                    {
                        c.MaterialName = material.Name;
                        c.MaterialName1 = material.Name;
                    }
                });
            }
            return result;
        }
    }
}
